package com.shiliao.app.activity.index.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.shiliao.app.R;
import com.shiliao.app.manager.NetworkManager;
import com.shiliao.app.utils.ShareUtils;
import com.shiliao.app.utils.YMServerUtil;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.yimei.framework.network.ICallBack;
import com.yimei.framework.util.LogUtil;
import com.yimei.framework.util.ToastUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yinzhiqiang on 2017/10/11.
 */

public class InviteFriendDialog extends Dialog implements View.OnClickListener {
    private TextView weixinFriends;
    private TextView weixinOnline;
    private TextView weibo;
    private TextView qqFriends;
    private TextView qqZone;
    private Window window;
    private TextView bt_cancel;
    private TextView lianjie;
    private Context mContext;
    private String mUrl;
    private String title;
    private String picUrl;
    private String sharDescription;
    private String uesrId;//用户id
    private String content_type;//是问题还是文章
    private String id;//问题id,或文章id
    private SHARE_MEDIA[] media = {SHARE_MEDIA.SINA, SHARE_MEDIA.QQ,
            SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE};

    public InviteFriendDialog(Context context, String url, String title,
                              String picUrl, String sharDescription, String userId,
                              String content_type, String id) {
        super(context, R.style.ShareDialogStyle);
        this.mContext = context;
        this.mUrl = url;
        this.title = title;
        this.picUrl = picUrl;
        this.sharDescription = sharDescription;
        this.uesrId = userId;
        this.content_type = content_type;
        this.id = id;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        WindowManager windowManager = ((Activity) mContext).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        params.width = (int) (display.getWidth());
        window.setAttributes(params);
        weixinFriends = (TextView) findViewById(R.id.text_weixin_friends);
        weixinOnline = (TextView) findViewById(R.id.text_weixin_online);
        weibo = (TextView) findViewById(R.id.text_weibo);
        qqFriends = (TextView) findViewById(R.id.text_qq_friends);
        qqZone = (TextView) findViewById(R.id.text_qqzone);
        bt_cancel = (TextView) findViewById(R.id.bt_cancel);
        lianjie = (TextView) findViewById(R.id.text_lianjie);
        setOnclickListener();
    }

    public void setOnclickListener() {
        weixinFriends.setOnClickListener(this);
        weixinOnline.setOnClickListener(this);
        weibo.setOnClickListener(this);
        qqFriends.setOnClickListener(this);
        qqZone.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        lianjie.setOnClickListener(this);
    }

    public void request() {
        Map map = new HashMap();
        if (!TextUtils.isEmpty(uesrId)) {
            map.put("user_id", uesrId);
        }
        if (!TextUtils.isEmpty(content_type)) {
            map.put("content_type", content_type);
        }
        if (!TextUtils.isEmpty(id)) {
            map.put("id", id);
        }
        NetworkManager.loadRequsetByRaw(YMServerUtil.ACTION_SHARE_SUCCESS_GETONTRIBUTION, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                LogUtil.e("yzq", result.toString());
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_weibo:
                if (isWxInstall(mContext)) {
                    ShareUtils.toSharedWX(media[0], (Activity) mContext, mUrl, title, sharDescription, picUrl);
                    request();
                } else {
                    Toast.makeText(mContext, "没有安装相关应用", Toast.LENGTH_SHORT).show();
                }
                dismiss();
                break;
            case R.id.text_qq_friends:
                ShareUtils.toSharedWX(media[1], (Activity) mContext, mUrl, title, sharDescription, picUrl);
                request();
                dismiss();
                break;
            case R.id.text_qqzone:
                ShareUtils.toSharedWX(media[2], (Activity) mContext, mUrl, title, sharDescription, picUrl);
                request();
                dismiss();
                break;
            case R.id.text_weixin_friends:
                ShareUtils.toSharedWX(media[3], (Activity) mContext, mUrl, title, sharDescription, picUrl);
                request();
                dismiss();
                break;
            case R.id.text_weixin_online:
                ShareUtils.toSharedWX(media[4], (Activity) mContext, mUrl, title, sharDescription, picUrl);
                request();
                dismiss();
                break;
            case R.id.bt_cancel:
                dismiss();
                break;
            case R.id.text_lianjie:
                ShareUtils.copy(mUrl, mContext);
                ToastUtils.toast("复制成功");
                dismiss();
                break;

            default:
                break;

        }
    }

    public static boolean isWxInstall(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.sina.weibo")) {
                    return true;
                }
            }
        }

        return false;
    }
}
